package com.ktjy.ktmall.servlet;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.ktjy.ktmall.entity.*;
import com.ktjy.ktmall.service.AddressService;
import com.ktjy.ktmall.service.UserService;
import com.ktjy.ktmall.service.impl.UserServiceImpl;
import com.ktjy.ktmall.vo.CartItemVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "UserServlet", urlPatterns = "/uni/manager/user")
public class UserServlet extends HttpServlet {
    private UserService service = null;

    @Override
    public void init() throws ServletException {
        service = new UserServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.setHeader("Access-Control-Allow-Origin","*");
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、创建out对象
        PrintWriter out = response.getWriter();
        // 2、获取从小程序端请求过来的参数
        String params = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
        System.out.println(params);
        // 3、将请求参数字符串转换成JSONObject对象，之后从JSONObject对象中取出值
        JSONObject entries = JSONUtil.parseObj(params);
        String opr = entries.get("opr", String.class);
        AjaxResult result = null;
        //通过opr区分当前操作是查询、删除、增加等等
        //opr=list
        switch (opr) {
            case "list":
                //执行查询
                List<User> users = service.selectUserList();
                result = AjaxResult.success("用户列表获取成功!", users);
                break;
            case "page": {
                //分页查询
                // 3、接收从小程序传递过来的当前页码currPageNo和每页显示的数据行数pageSize
                int currPageNo = 1;  // 当前页码

                if (request.getParameter("currPageNo") != null) {
                    currPageNo = Integer.valueOf(request.getParameter("currPageNo"));
                }
                int pageSize = 5;  // 每页显示的数据行数

                if (request.getParameter("pageSize") != null) {
                    pageSize = Integer.parseInt(request.getParameter("pageSize"));
                }
                // 4、调用serivce中查询总记录数的方法
                int count = service.getCount();
                // 5、调用service中查询商品的分页列表数据
                List<User> list = service.getUserListPage(currPageNo, pageSize);
                // 6、封装Page对象
                Page page = new Page();
                page.setCurrPageNo(currPageNo);
                page.setPageSize(pageSize);
                page.setTotalCount(count);
                page.setList(list); //

                result = AjaxResult.success("用户列表加载成功!", page);
                break;
            }
            case "insert": {
                User user = entries.get("user", User.class);
                System.out.println(user.toString());
                int count = service.insertUser(user);
                if (count > 0) {
                    result = AjaxResult.success("新增成功!");
                } else {
                    result = AjaxResult.success("新增失败!");
                }
                break;
            }
            case "selectUserById":
                Integer id = Integer.valueOf(entries.get("id", Integer.class));
                User user = service.selectUserById(id);
                result = AjaxResult.success("用户列表查询成功", user);
                break;
            case "update":
                user = entries.get("user", User.class);
                // 3.2 调用service中的新增方法
                int i = service.updateUser(user);
                if (i > 0) {
                    result = AjaxResult.success("修改成功!");
                } else {
                    result = AjaxResult.error("修改失败!");
                }
                break;
            case "delete":
                user = entries.get("user", User.class);
                i = service.deleteUser(user);
                if (i > 0) {
                    result = AjaxResult.success("删除成功!");
                } else {
                    result = AjaxResult.error("删除成功!");
                }
                break;
            case "login":
                user = entries.get("user", User.class);
                User u = service.login(user);
                if (u != null) {
                    result = AjaxResult.success("登录成功!",u);
                } else {
                    result = AjaxResult.error("登录失败!");
                }
                break;
        }
        // 将result对象转换成JSON字符串再响应到小程序端
        out.print(JSONArray.toJSONString(result));
        out.flush();
        out.close();
    }
}
